package com.edou.member.controller.mobile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.edou.base.controller.BaseController;
import com.edou.base.util.VerifyCode;
import com.edou.member.entity.MemberUser;
import com.edou.member.form.MemberUserForm;
import com.edou.member.service.IMemberUserService;

/**
 * 会员首页模块
 * @creator     admin
 * @create-time 2016/3/12 16:10:22
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
@Controller("memberUserRegistMobileController")
@Scope("prototype")
@RequestMapping("/app/member")
public class MemberUserRegistMobileController extends BaseController {
	
	@Value("${domain.path}")
	private String domainPath;
	
	@Resource(name = "memberUserServiceImpl")
	private IMemberUserService memberUserService;
	
	/**
	 * 注册验证：帐号唯一性验证
	 * @return 
	 */
	@RequestMapping(value = "/check_account.htm", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView checkAccount(MemberUserForm model){
		String account = model.getAccount();
		MemberUser member = memberUserService.getMemberUserByAccount(account);
		if (null == member) {
			return ajaxJSON(Status.success,"true");
		} else {
			return ajaxJSON(Status.success,"false");
		}
	}
	
	/**
	 * 注册验证：邮箱唯一性验证
	 * @return 
	 */
	@RequestMapping(value = "/check_email.htm", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView checkEmail(MemberUserForm model){
		String email = model.getEmail();
		MemberUser member = memberUserService.getMemberUserByEmail(email);
		if (null == member) {
			return ajaxJSON(Status.success,"true");
		} else {
			return ajaxJSON(Status.success,"false");
		}
	}
	
	/**
	 * 注册验证：手机唯一性验证
	 * @return 
	 */
	@RequestMapping(value = "/check_mobile.htm", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView checkMobile(MemberUserForm model){
		String mobile = model.getMobile();
		MemberUser member = memberUserService.getMemberUserByMobile(mobile);
		if (null == member) {
			return ajaxJSON(Status.success,"true");
		} else {
			return ajaxJSON(Status.success,"false");
		}
	}
	
	/**
	 * 生产验证码
	 * @return 
	 */
	@RequestMapping(value = "/verification.htm", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView verification(MemberUserForm model,HttpSession session,HttpServletResponse response){
		//1.获取随机数
		String verifyCodeStr = VerifyCode.generateTextCode(VerifyCode.TYPE_NUM_ONLY, 6, null);
		logger.info("生成的验证码：" + verifyCodeStr);
		
		//3.发送短信
		String message = "验证码：" + verifyCodeStr + "，请勿向任何人泄漏此验证码。如非本人操作，请忽略此消息。";
		
		// ========== 特别注意  =========
		// 此处用于接入短信接口之前的测试方案，前台能够取到验证码。
		// 短信接口接入后，需要将message去除，并注意将前台的alert去除，否则有安全隐患。
		// ============================
	
		return ajaxJSON(Status.success,verifyCodeStr);
	}
	
	/**
	 * 临时生成验证码
	 * 说明：在没有开通手机验证码的情况下，可先使用该方法
	 * @return 
	 */
	@RequestMapping(value = "/temp_ver_code.htm", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView tempVerCode(MemberUserForm model,HttpSession session,HttpServletResponse response){
		//1.获取随机数
		String verifyCodeStr = VerifyCode.generateTextCode(VerifyCode.TYPE_NUM_ONLY, 6, null);
		logger.info("生成的验证码：" + verifyCodeStr);
		
		//3.发送短信
		String message = "验证码：" + verifyCodeStr + "，请勿向任何人泄漏此验证码。如非本人操作，请忽略此消息。";
		
		return ajaxJSON(Status.success,message);
	}
	
	/**
	 * 跳转到注册成功页
	 * @return 
	 */
	@RequestMapping(value = "/regist.htm", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView regist(MemberUserForm model,HttpSession session,HttpServletResponse response){
		//获取用户信息
		MemberUser item = model.getItem();
		//设置用户头像
		if(StringUtils.isBlank(item.getFacePath())){
			//给默认图片
			String facePath = domainPath + "/static/mobile/base/images/head.png";
			item.setFacePath(facePath);
		}
		if(StringUtils.isBlank(item.getNickName())){
			item.setNickName(item.getAccount());
		}
		if(StringUtils.isBlank(item.getName())){
			item.setName(item.getNickName());
		}
		//添加用户
		memberUserService.add(item);
	
		return ajaxJSON(Status.success, "注册成功");
	}
	
	
	
}